﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
using System.Text;
using System.IO;
using System.Web;

using log4net;
using log4net.Config;
using EnglishRemix.Services.Model.Exceptions;
using EnglishRemix.Services.Model.Facades;
using EnglishRemix.Services.Model;

//a one-time entry that needs to be placed outside of your class
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "/Web.config", Watch = true)]

namespace EnglishRemix.Services
{
    // Start the service and browse to http://<machine_name>:<port>/Service1/help to view the service's generated help page
    // NOTE: By default, a new instance of the service is created for each call; change the InstanceContextMode to Single if you want a single instance of the service to process all calls.

    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]

    // NOTE: If the service is renamed, remember to update the global.asax.cs file
    public partial class EnglishRemixBookService : IEnglishRemixBookService
    {
        // Create a logger for use in this class
        private static log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        public UserFacade userFacade = new UserFacade();
        public DeviceFacade deviceFacade = new DeviceFacade();

        public EnglishRemixBookService()
        {
            log4net.Config.XmlConfigurator.Configure();
            log.Debug("EnglishRemixBookService is constructed.");
        }

        [WebGet(UriTemplate = "/test/id={id}", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
        public String testRequest(String id)
        {
            log.Debug("EnglishRemix2: testRequest service is alive.");

            Test testFacade = new Test();

            Response response = new Response();
            response.result = "200";
            response.message = testFacade.getUser(id);

            return response.toJson();
        }

        [WebGet(UriTemplate = "/", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
        public String getAlive()
        {
            log.Debug("EnglishRemix2: GET service is alive.");
            return "EnglishRemix2: GET service is alive.";
        }

        [WebInvoke(UriTemplate = "/", Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
        public String postAlive()
        {
            log.Debug("EnglishRemix2: POST service is alive.");
            return "EnglishRemix2: POST service is alive.";
        }

        [WebInvoke(UriTemplate = "/", Method = "PUT", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
        public String putAlive()
        {
            log.Debug("EnglishRemix2: PUT service is alive.");
            return "EnglishRemix2: PUT service is alive.";
        }

        [WebInvoke(UriTemplate = "/", Method = "DELETE", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
        public String deleteAlive()
        {
            log.Debug("EnglishRemix2: DELETE service is alive.");
            return "EnglishRemix2: DELETE service is alive.";
        }

        public NameValueCollection getQueryArray(Stream input)
        {
            StreamReader reader = new StreamReader(input);
            string queryString = reader.ReadToEnd();
            NameValueCollection queryArray = HttpUtility.ParseQueryString(queryString);
            return queryArray;
        }

        //public bool validateDevice(String deviceId)
        //{
        //    bool isActivated = false;

        //    String validationResult = deviceFacade.getDeviceStatus(deviceId).result;

        //    isActivated = (validationResult.Equals("200")) ? true : false;

        //    return isActivated;
        //}

        public void validateDevice(String deviceId)
        {
            bool isActivated = false;

            String validationResult = deviceFacade.getDeviceStatus(deviceId).result;

            isActivated = (validationResult.Equals("200")) ? true : false;

            if (!isActivated)
            {
                throw new DeviceValidationException();
            }
        }
    }
}
